class Site < ActiveRecord::Base
  has_many :site_posts
  belongs_to :user
  
  #belongs_to :created_user, :class_name => "User", :foreign_key => "created_user_id"
    
  #has_and_belongs_to_many :communities
  has_many :community_sites, :dependent => :destroy
  has_many :communities, :through => :community_sites
  
  belongs_to :site_category
  
  validates_presence_of     :title, :message => "名称不能为空"
  validates_presence_of     :description, :message => "描述不能为空"
  validates_length_of       :title,    :within => 2..100, :too_long => "名称在2到50位之间", :too_short => "名称在2到50位之间"
  validates_length_of       :description,    :within => 2..2000, :too_long => "描述在2到2000位之间", :too_short => "描述在2到2000位之间"
  
  file_column :image, :magick => { 
          #:versions => { "thumb" => "40x40", "medium" => "100x80>" }
          :versions => { 
          		:square => { :crop => "1:1", :size => "40x40", :name => "thumb"},
          		:screen => {:crop => "1:1", :size => "100x80", :name => "medium"},
          }
  }
  
  analyzer = Ferret::Analysis::PerFieldAnalyzer.new(Ferret::Analysis::StandardAnalyzer.new)
  analyzer[:title] = Ferret::Analysis::RegExpAnalyzer.new(/./, false)
  analyzer[:description] = Ferret::Analysis::RegExpAnalyzer.new(/./, false)
  analyzer[:biz_contact] = Ferret::Analysis::RegExpAnalyzer.new(/./, false)
  analyzer[:biz_place] = Ferret::Analysis::RegExpAnalyzer.new(/./, false)
  
  acts_as_ferret({:fields => {:title => {:store => :yes}, :description => {:store => :yes}, :biz_contact => {:store => :yes}, :biz_place => {:store => :yes}}}, {:analyzer=> analyzer})
  
  def neighbor_same_cat_sites
    communities = self.communities
    query = ""
    query << "SELECT *  FROM `sites` inner join"
    query << " `community_sites` on community_id in ('"
    query << communities.collect{|n| n.id}.join(',')
    query << "') where "
    query << " id != "
    query << self.id.to_s
    query << " and site_category_id = "
    query << self.site_category_id.to_s
    query << " group by id"
    sites = Site.find_by_sql [query]
    sites
  end
  
  def neighbor_sites_by_cat(cat)
    communities = self.communities
    query = ""
    query << "SELECT sites.*  FROM `sites`,`community_sites` where community_sites.community_id in ('"
    query << communities.collect{|n| n.id}.join(',')
    query << "') and community_sites.site_id = sites.id "
    query << " and sites.id != "
    query << self.id.to_s
    query << " and site_category_id = "
    query << cat.id.to_s
    query << " group by sites.id"
    sites = Site.find_by_sql [query]
    sites
  end
  
  def neighbor_sites
    communities = self.communities
    query = ""
    query << "SELECT sites.*  FROM `sites`,`community_sites` where community_sites.community_id in ('"
    query << communities.collect{|n| n.id}.join(',')
    query << "') and community_sites.site_id = sites.id "
    query << " and sites.id != "
    query << self.id.to_s
    query << " group by sites.id"
    sites = Site.find_by_sql [query]
    sites
  end
  
  def can_edit?(user)
    return false if user.nil?
    return true if user == self.user
    
    for comm in self.communities
      if comm == user.community &&  user.scores <= 5000
        return true
      end
    end
    
    return false
  end
  
  def before_save
    self.title = self.title.gsub(/^\<(script|frame)/i, '')
    self.biz_contact = self.biz_contact.gsub(/^\<(script|frame)/i, '')
    self.biz_place = self.biz_place.gsub(/^\<(script|frame)/i, '')
    self.description = self.description.gsub(/^\<(script|frame)/i, '')
  end
end
